<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'../system/config.php';
require_once $INCLUDE_DIR.'cametrics.helper.php';
$url = Cametrics::getURL();
$global_flag = false;

if (!isset($_REQUEST['username']) || $_REQUEST['username'] == '') {
	$global_flag = true;
}
else {
	$username = $_REQUEST['username'];
}
if (!isset($_REQUEST['password']) || $_REQUEST['password'] == '') {
	$global_flag = true;
}
else {
	$password = md5($_REQUEST['password']);
}

// fail on database errors
try {
	$db = new PDO("sqlite:".$DATABASE);
} catch (PDOException $e) {
	header('HTTP/1.1 500 Internal Server Error');
	die('Sorry, there is a problem with the database');
}

if (!$global_flag) {
	$query = "SELECT username, password FROM user WHERE username = :username";
	$stmt = $db->prepare($query);
	$stmt->bindParam(':username', $username);
	$stmt->execute();

	$result = $stmt->fetch(PDO::FETCH_ASSOC);
	if ($result === false) {
		$global_flag = true;
	}
	if ($result['password'] != $password) {
		$global_flag = true;
	}
}

$output = array();

if (!$global_flag) {
	$query = "SELECT dttaken, tags, latitude, longitude, squareimg, thumbimg, smallimg, mediumimg, originalimg FROM images WHERE username = ? ORDER BY dttaken DESC LIMIT 0,5";

	$stmt = $db->prepare($query);

	$stmt->execute(array($username));

	$result = $stmt->fetchall(PDO::FETCH_ASSOC);

	if ($result) {
		$output['last_five_obs'] = array();
		foreach($result as $row) {
			$row['latitude'] = floatval($row['latitude']);
			$row['longitude'] = floatval($row['longitude']);

			$output['last_five_obs'][] = $row;
		}
	}

	$query = "SELECT COUNT(id) as cnt FROM images WHERE username = ?";

	$stmt = $db->prepare($query);

	$stmt->execute(array($username));

	$result = $stmt->fetch(PDO::FETCH_ASSOC);
	if ($result) {
		$output['num_user_images']=$result['cnt'];
	}

	$output['num_user_contributions'] = @file_get_contents("$url/$username/contributions/type/stats/count");

	$raw = @file_get_contents("$url/$username/contributions/type/stats/hits");
	$type_hits = json_decode($raw, true);
	$output['user_species_breakdown'] = $type_hits;
}

$raw = @file_get_contents("$url/contributions/stats/hits?order=desc");
$hits = json_decode($raw, true);
$count = count($hits);

$top = array();
foreach (range(0,2) as $i){
	if (isset($hits[$i])){
		$top[$i]['user'] = $hits[$i][0];
		$top[$i]['count'] = $hits[$i][1];
	}
}

if (!$global_flag) {
	$rank = 1;
	$userfound = false;
	foreach ($hits as $row) {
		if ($row[0] == $username) {
			$userfound = true;
			break;
		}
		else {
			$rank++;
		}
	}
	if ($userfound) {
		$output['rank'] = $rank;
	}
	else {
		$output['rank'] = 'user not yet ranked';
	}
}

$lastfive = array();
$num_images = @file_get_contents("$url/contributions/stats/count");

$output['total_num_images'] = (int)$num_images;
$output['num_users'] = $count;
$output['top_users'] = $top;
$db = null;

echo json_encode($output);
?>
